package fr.sharpe.app.earthvisualizationproject.ui;


import gov.nasa.worldwind.awt.WorldWindowGLCanvas;
import gov.nasa.worldwind.examples.util.SectorSelector;
import gov.nasa.worldwind.geom.LatLon;
import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.layers.MarkerLayer;
import gov.nasa.worldwind.render.markers.Marker;
import gov.nasa.worldwind.util.StatisticsPanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.JFrame;


public class EarthView extends JFrame implements KeyListener{

	private static WorldWindowGLCanvas wwd;
	private MarkerLayer layer;
	private ArrayList<Marker> markers;
	private StatisticsPanel statsPanel;

	public EarthView()
	{

		this.setTitle("Earth View");
		this.setPreferredSize(new java.awt.Dimension(1000, 800));

		wwd = Earth.getInstance().getWwd();
		wwd.addKeyListener(this);
		this.getContentPane().add(wwd, java.awt.BorderLayout.CENTER);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);

		this.pack();
	}

	public void setFullScreen()
	{
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		System.out.println(dim);
		this.dispose();
		this.setUndecorated(true);
		this.setPreferredSize(dim);
		this.setMinimumSize(dim);
		this.setMaximumSize(dim);
		this.setExtendedState(Frame.MAXIMIZED_BOTH);
		this.setVisible(true);
		this.pack();
	}

	public void setNormalScreen()
	{
		this.dispose();
		this.setUndecorated(false);
		this.setExtendedState(Frame.NORMAL);
		this.setVisible(true);
		this.pack();
	}

	public void update()
	{
		wwd.update(getGraphics());
	}

	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		//		System.out.println(e.getKeyChar());
	}

	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		//		System.out.println(e.getKeyChar());
	}

	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		System.out.println(e.getKeyChar());
		if(e.getKeyChar()=='f')
		{
			System.out.println("fullscreen");
			setFullScreen();
		}
		if(e.getKeyChar()=='n')
		{
			System.out.println("normal");
			setNormalScreen();
		}
		if(e.getKeyChar()=='h')
		{
			System.out.println("Print help");
			//TODO Dialogbox with help
		}
		if(e.getKeyChar()=='c')
		{
			System.out.println("Moving");
			Position centerPos = new Position(LatLon.fromDegrees(0, 0),0);
			wwd.getView().stopAnimations();
			wwd.getView().goTo(centerPos,1000e3);
			
		}
		
		if(e.getKeyChar()=='s')
		{
			System.out.println("Select Zone");
			SectorSelector selector;
			
			selector = new SectorSelector(wwd);
            selector.setInteriorColor(new Color(1f, 1f, 1f, 0.1f));
            selector.setBorderColor(new Color(1f, 0f, 0f, 0.5f));
            selector.setBorderWidth(3);
			selector.enable();
		}

	}

}
